package com.wangfei.controller;

import com.wangfei.annotation.ManageRestController;
import com.wangfei.entity.User;
import com.wangfei.exception.ApiException;
import com.wangfei.helper.utils.Result;
import com.wangfei.service.UserService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@ManageRestController
public class MIndexController {

    @Autowired
    UserService userService;

    @GetMapping("/")
    public Result index(@RequestParam(value = "name", defaultValue = "") String name)
    {
        if ("e".equals(name)) {
            throw new ApiException("异常测试");
        }
        List<User> list = userService.getList(new User());
        // 移除里面openid
        for (User user : list) {
            user.setOpenid(null);
        }
//        int i = 3 / 0;
        return Result.success(list);
    }

    @PostMapping("/user/add")
    public Result add(@Valid @RequestBody User user) {
        userService.insert(user);
        return Result.success();
    }

    @GetMapping("/2")
    public Result index2() {
        return Result.success();
    }

    @GetMapping("/3")
    public Result index3() {
        return Result.error();
    }

    @GetMapping("/4")
    public Result index4() {
        return Result.error("templates/error");
    }

    @GetMapping("/user/info")
    public Result users(@RequestParam Integer id) {
        User user = userService.getById(id);
        userService.withUserAddress(user);
        return Result.success(user);
    }
}
